home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11869 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: newshost.cin.gov.au!usenet
  2. From: rossw@cin.gov.au (Ross Wilson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: goto
  5. Date: Wed, 27 Mar 1996 01:51:03 GMT
  6. Organization: Community Information Network
  7. Message-ID: <4ja6u3$8v5@canb.cin.gov.au>
  8. References: <Pine.OSF.3.91.960313102715.10701D-100000@io.UWinnipeg.ca>
  9. NNTP-Posting-Host: chico.cin.gov.au
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Bill Simpson <wsimpson@uwinnipeg.ca> wrote:
  13.  
  14. >There was a goto thread lately, and my problem is to state this algorithm 
  15. >cleanly without gotos (which I think is easy, but my attempts have been 
  16. >failures).
  17.  
  18. >0. x=0;
  19. >1. x+=erand(maxmean);
  20. >2. if (urand2()>rate(x)/maxrate)
  21. >    goto step 1
  22. >3. if (x<=XMAX)
  23. >    {
  24. >    setdot(x,y,z);
  25. >    goto step 1
  26. >    }
  27.  
  28. <explanations of erand(), etc, snipped>
  29.  
  30. One simple translation might be:
  31.  
  32.     x = 0;
  33.     for (;;)
  34.     {
  35.         x += erand(maxmean);
  36.         if (urand2() > rate(x) / maxrate)
  37.             continue;
  38.         if (x <= XMAX)
  39.         {
  40.             setdot(x, y, z);
  41.             continue;
  42.         }
  43.         break;
  44.     }
  45.  
  46. but it looks kind of clumsy to have a break last thing in the loop.
  47. A slight remolding gives:
  48.  
  49.     x = 0;
  50.     for (;;)
  51.     {
  52.         x += erand(maxmean);
  53.         if (urand2() > rate(x) / maxrate)
  54.             continue;
  55.         if (x > XMAX)
  56.             break;
  57.         setdot(x, y, z);
  58.     }
  59.  
  60. which looks a little better.
  61.  
  62. I don't think there is any really good, universally acceptable answer
  63. to this sort of thing.
  64.  
  65. Ross
  66.  
  67.